﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gol.Domain;

namespace Gol.UI.Winforms
{
    // "inspired" by code in Dr. GUI .NET #7 http://msdn.microsoft.com/en-us/library/aa302318.aspx
    public partial class FormView : Form, IFormView
    {
        private int _imgWidth;
        private int _imgHeight;
        private int _rows;
        private int _cols;
        private int _widthCell;
        private int _heightCell;
        private Graphics _g;
        private Image _img;
        private readonly FormPresenter _presenter;

        public FormView()
        {
            InitializeComponent();
            _presenter = new FormPresenter(this);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _presenter.Initialize();
        }

        private Image DrawBoard(CellGrid cellGrid)
        {

            _g.FillRectangle(Brushes.Black, 0, 0, _imgWidth, _imgHeight);


            for (int i = 0; i < _rows - 1; i++)
            {
                for (int j = 0; j < _cols - 1; j++)
                {
                    if (cellGrid[i, j].IsAlive)
                    {
                        _g.FillEllipse(Brushes.HotPink, j * _widthCell, i * _heightCell, _widthCell, _heightCell);
                    }
                }
            }

            return _img;

        }

        private void ButtonStepClick(object sender, EventArgs e)
        {
            this._presenter.Step();

        }
        
        private void Timer1Tick(object sender, EventArgs e)
        {
            _presenter.NextState();
        }

        private void ButtonAnimateClick(object sender, EventArgs e)
        {
            this._presenter.Animate();
        }

        private void ButtonRandomizeClick(object sender, EventArgs e)
        {

            this._presenter.Randomize();

        }

        private int[,] GetLifeOnes(int max)
        {
            var lifeOnes = new int[max, 2];
            Random r = new Random();

            for (int i = 0; i < max; i++)
            {
                lifeOnes[i, 0] = r.Next(this._rows);
                lifeOnes[i, 1] = r.Next(this._cols);
            }
            return lifeOnes;
        }

        public int[,] GetBoardSize()
        {
            _rows = 120;
            _cols = 120;
            return new int[,] { { _rows, _cols } };
        }

        public void SetupGrid()
        {
            _imgWidth = pictureBoxGrid.ClientSize.Width;
            _imgHeight = pictureBoxGrid.ClientSize.Height;
            _widthCell = _imgWidth / (_cols);
            _heightCell = _imgHeight / (_rows);
            _imgWidth = _widthCell * _cols;
            _imgHeight = _heightCell * _rows;
            if (_img != null)
            {
                _img.Dispose();
                _g.Dispose();
            }

            _img = new Bitmap(_imgWidth, _imgHeight);

            _g = Graphics.FromImage(_img);


        }
   
        public void ShowGridContent(CellGrid cellGrid)
        {
            pictureBoxGrid.Image = DrawBoard(cellGrid);
        }
        
        public void StopTimer()
        {
            this.timer1.Stop();
            this.timer1.Enabled = false;
        }

        public void StartTimer()
        {
            this.timer1.Enabled = true;
            this.timer1.Start();
        }
    }
}
